package com.woniuxy.shop.controller;


import com.woniuxy.shop.service.UserService;
import com.woniuxy.shop.utils.CookieUtils;
import com.woniuxy.shop.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.JstlUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fanqiwen
 * @since 2023-02-07 11:56:02
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;


    @RequestMapping("reg")
    public String reg(String username, String password, String repass) {
        try {
            userService.reg(username, password, repass);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "ok";
    }

    @RequestMapping("login")
    public String login(String username, String password, HttpServletResponse response) {
        try {
            //验证密码和账号
            userService.loginCheck(username, password);
            String token = JwtUtils.createToken(username, 15);
            CookieUtils.setUserToken2Cookie(response, token);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "ok";
    }

    @RequestMapping("loginOut")
    public void loginOut(HttpServletResponse response) {
        CookieUtils.deleteUserTokenFromCookie(response);
    }

    @RequestMapping("getCurrentName")
    public String getCurrentName(HttpServletRequest request ) {
        String token = CookieUtils.getUserToken2Cookie(request);
        String account=null;
        try {
            account = JwtUtils.getAccount(token);
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return account;
    }

    @RequestMapping("addCookie")
    public String addCookie(String name, String value, HttpServletResponse response) {

        //创建Cookie对象
        Cookie cookie = new Cookie(name, value);
        // domain + path  确定了cookie的生效范围
        //cookie.setDomain(""); //domain 域名   可以自己指定域名,也可以不写,不写的话默认就是当前网站的地址.
        cookie.setPath("/"); //在域名范围内的那些路径下是有效的   / 表示在当前网站下都有效
        cookie.setMaxAge(160);//设置cookie的有效期,单位秒
        cookie.setComment("测试cookie");

        response.addCookie(cookie);

        return "ok";
    }


    @RequestMapping("queryCookie")
    public String queryCookie(HttpServletRequest request) {

        Cookie[] cookies = request.getCookies();
        for (int i = 0; cookies != null && i < cookies.length; i++) {
            System.out.println(cookies[i]);
        }

        return "ok";
    }


    @RequestMapping("updateCookie")
    public String updateCookie(String name, String value, HttpServletResponse response) {

        //更新cookie  重新写一遍

        //创建Cookie对象
        Cookie cookie = new Cookie(name, value);
        // domain + path  确定了cookie的生效范围
        //cookie.setDomain(""); //domain 域名   可以自己指定域名,也可以不写,不写的话默认就是当前网站的地址.
        cookie.setPath("/"); //在域名范围内的那些路径下是有效的   / 表示在当前网站下都有效
        cookie.setMaxAge(160);//设置cookie的有效期,单位秒   如果为0,就表示删除
        cookie.setComment("测试cookie");

        response.addCookie(cookie);

        return "ok";
    }


    @RequestMapping("delCookie")
    public String delCookie(String name, String value, HttpServletResponse response) {

        //删除cookie  cookie.setMaxAge(0)

        //创建Cookie对象
        Cookie cookie = new Cookie(name, value);
        // domain + path  确定了cookie的生效范围
        //cookie.setDomain(""); //domain 域名   可以自己指定域名,也可以不写,不写的话默认就是当前网站的地址.
        cookie.setPath("/"); //在域名范围内的那些路径下是有效的   / 表示在当前网站下都有效
        cookie.setMaxAge(0);//设置cookie的有效期,单位秒   如果为0,就表示删除
        cookie.setComment("测试cookie");

        response.addCookie(cookie);

        return "ok";
    }
}

